package com.burpee.web;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.burpee.service.BoardItemsService;
import com.burpee.service.BoardService;
import com.burpee.ui.Board;
import com.burpee.ui.Boarditems;
import com.burpee.ui.Member;
@Controller
@RequestMapping("/board.spring")
public class BoardController {
	
	@Autowired
	BoardService boardService;
//	
//	@Autowired
//	AddfileService addfileService;
//	
	@Autowired
	BoardItemsService boardItemsService;
//	
//	@Autowired
//	CommentsService commentsService;
	
	@RequestMapping(params = "action=premodify")
	public ModelAndView premodify(HttpServletRequest request, HttpServletResponse response, Board board){
		
		board = boardService.view(board.getBoardNo());
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("board", board);
		modelAndView.setViewName("board/boardmodify");
		
		return modelAndView;		
		
	}
	
///////////////////		일반 게시판의 게시글 리스트
	@RequestMapping(params = "action=boarditemsList")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse respons){
		/////
//		List<Boarditems> boarditemsList2 = boardItemsService.list(0, null, null, 0);
		
		List<Boarditems> boarditemsList = boardItemsService.list2();
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("boardItems", boarditemsList);
		modelAndView.setViewName("bbs/boarditemsList");
		return modelAndView;
		
	}
	
	@RequestMapping(params = "action=boarditemsView")
	public ModelAndView view(HttpServletRequest request, HttpServletResponse respons){
		/////
//		Boarditems boarditems = boardItemsService.countBoardItem(boardListNo, searchCondition, searchTitle);
		
		ModelAndView modelAndView = new ModelAndView();
//		modelAndView.addObject("boardItems", boarditemsList);
		modelAndView.setViewName("bbs/boarditemsView");
		return modelAndView;
		
	}
	
	@RequestMapping(params = "action=boarditemsImagelist")
	public ModelAndView imagelist(HttpServletRequest request, HttpServletResponse respons){
		//////
		List<Boarditems> boarditemsList = boardItemsService.list(0, null, null, 0);
		
		ModelAndView modelAndView = new ModelAndView();
//		modelAndView.addObject("boardItems", boarditemsList);
		modelAndView.setViewName("bbs/albumBBS");
		
		return modelAndView;
		
	}
	
	@RequestMapping(params = "action=boardItemsPremodify")
	public ModelAndView boardItemsPremodify(long boardItemsNo){

		Boarditems boarditems=boardItemsService.view(boardItemsNo);

		ModelAndView modelAndView = new ModelAndView();

		modelAndView.addObject("boardItems",boarditems);

		modelAndView.setViewName("bbs/boarditemsModify");

		return modelAndView;

	}
	@RequestMapping(params="action=boardItemsModify")
	public ModelAndView boardItemsModify(Boarditems boarditems)
	{
		boarditems.setUpdateDate(new Timestamp(System.currentTimeMillis()));

		if (boarditems.getBoarditemsRemoveYn()==null) {

			boarditems.setBoarditemsNoticeYn("N");

		} else {

			boarditems.setBoarditemsNoticeYn("Y");

		}

		boolean result=boardItemsService.modify(boarditems);

		ModelAndView modelAndView =new ModelAndView();

		if (result) {

			boarditems=boardItemsService.view(boarditems.getBoarditemsNo());

			modelAndView.addObject("boarditems",boarditems);

			modelAndView.setViewName("bbs/boarditemsModify");

		} else {

		}
		return modelAndView;

	}
	
//	@RequestMapping(params = "action=boarditemsPreModify")
//	public ModelAndView boarditemsPreModify(HttpServletRequest request, HttpServletResponse respons){
//		
//		ModelAndView modelAndView = new ModelAndView();
////		modelAndView.addObject("boardItems", boarditemsList);
//		modelAndView.setViewName("bbs/boarditemsModify");
//		
//		return modelAndView;
//		
//	}
//	@RequestMapping(params = "action=modify")
//	public ModelAndView modify(HttpServletRequest request, HttpServletResponse response, Board board){
//		
//		boardService.modify(board);
//		
//		
//		board = boardService.view(board.getBoardNo());
//		ModelAndView modelAndView = new ModelAndView();
//		modelAndView.addObject("board", board);
//		modelAndView.setViewName("redirect:main.spring");
//		return modelAndView;
//		
//	}
	
	@RequestMapping(params = "action=boardItemsPreAdd")
	public ModelAndView boarditemsPreAdd(HttpServletRequest request, HttpServletResponse respons){

		ModelAndView modelAndView = new ModelAndView();

		modelAndView.setViewName("bbs/boarditems");

		return modelAndView;

	}


	@RequestMapping(params = "action=boardItemsAdd")
	public ModelAndView boarditemsAdd(HttpServletRequest request, HttpServletResponse respons,Boarditems boarditems){

		Member member = (Member)request.getSession().getAttribute("login");

		boarditems.setMemberId(member.getMemberId());

		if(boarditems.getBoarditemsNoticeYn() == null){
			boarditems.setBoarditemsNoticeYn("N");
		}
		else{
			boarditems.setBoarditemsNoticeYn("Y");
		}

		boarditems.setBoarditemsViewCnt(0);
		boarditems.setBoarditemsRemoveYn("N");
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());


		boarditems.setUpdateDate(timestamp);
		boarditems.setInputDate(timestamp);

		boolean result = boardItemsService.add(boarditems);

		if(result){

			int currentPage = 0;

			String strCurrentPage = request.getParameter("currenPage");

			if(strCurrentPage != null){
				currentPage = Integer.parseInt(strCurrentPage);
			}

			return list(request, respons);
		}
		else{

			ModelAndView modelAndView = new ModelAndView();
			//modelAndView.addObject("message", "입력에 실패하였습니다.");
			modelAndView.setViewName("bbs/freeBBS");

			return modelAndView;
		}
		//		ModelAndView modelAndView = new ModelAndView();
		//
		//		modelAndView.setViewName("bbs/freeBBS");
		//		
		//		return modelAndView;

	}
//	@RequestMapping(params = "action=boarditemsPreAdd")
//	public ModelAndView boarditemsPreAdd(HttpServletRequest request, HttpServletResponse respons){
//		
//		ModelAndView modelAndView = new ModelAndView();
////		modelAndView.addObject("boardItems", boarditemsList);
//		modelAndView.setViewName("bbs/boarditems");
//		
//		return modelAndView;
//		
//	}
//	
//	
//	@RequestMapping(params = "action=boarditemsAdd")
//	public ModelAndView boarditemsAdd(HttpServletRequest request, HttpServletResponse respons){
//		
//		//////
//		List<Boarditems> boarditemsList = boardItemsService.list(0, null, null, 0);
//		
//		ModelAndView modelAndView = new ModelAndView();
////		modelAndView.addObject("boardItems", boarditemsList);
//		modelAndView.setViewName("bbs/albumBBS");
//		
//		return modelAndView;
//		
//	}
	
	private String generatePaging(
			long boardListNo,
			int currentPage, 
			int totalCount,
			String searchCondition,
			String searchTitle) {
		
		if (searchCondition == null) {
			searchCondition = "";
		}
		
		if (searchTitle == null) {
			searchTitle = "";
		}
				
		int pageCount = totalCount / 20;
		
		if (totalCount % 20 != 0) {
			pageCount++;
		}
		
		int currentPageBlock = currentPage / 10;
		
		int startPage = currentPageBlock * 10 + 1;
		int maxPage = pageCount;
		
		if (startPage + 10 < pageCount) {
			maxPage = startPage + 9;
		}
		
		String url = "action=itemlist" +
				"&boardListNo=" + boardListNo + 
				"&searchCondition=" + searchCondition +
				"&searchTitle=" + searchTitle;
		
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<a href=\"javascript:itemListPaging('" + url + "&currentPage=1')\">" + "[泥섏쓬]</a>");

		if (currentPageBlock < 1) {
			stringBuffer.append("[<<]");
		}
		else {
			stringBuffer.append("<a href=\"javascript:itemListPaging('" + url + "&currentPage=" + (currentPageBlock * 10 - 9) + "')\">" + "[<<]</a>");
		}
		
		for (int i = startPage; i <= maxPage; i++) {
			
			if (i == currentPage) {
				stringBuffer.append("&nbsp;<b><a href=\"javascript:itemListPaging('" + url + "&currentPage=" + i + "')\">" + i + "</a></b>&nbsp;");
			}
			else {
				stringBuffer.append("&nbsp;<a href=\"javascript:itemListPaging('" + url + "&currentPage=" + i + "')\">" + i + "</a>&nbsp;");
			}
			
		}
		
		if (maxPage == pageCount) {
			stringBuffer.append("[>>]");
		}
		else {
			stringBuffer.append("<a href=\"javascript:itemListPaging('" + url + "&currentPage=" + (currentPageBlock * 10 + 11) + "')\">" + "[>>]</a>");
		}
		stringBuffer.append("<a href=\"javascript:itemListPaging('" + url + "&currentPage=" + pageCount + "')\">" + "[��</a>");
		
		return stringBuffer.toString();
	}

}

//@Controller
//@RequestMapping("/bbs.spring")
//public class BoardController {
//
//	@Autowired
//	BoardService boardService;
//
//	@Autowired
//	BoardItemsService boardItemsService;
//	//	
//	//	@Autowired
//	//	AddfileService addfileService;
//	//	
//
//	//	@Autowired
//	//	CommentsService commentsService;
//
//		@RequestMapping(params = "action=premodify")
//		public ModelAndView premodify(HttpServletRequest request, HttpServletResponse response, Board board){
//	
//			board = boardService.view(board.getBoardNo());
//	
//			ModelAndView modelAndView = new ModelAndView();
//			modelAndView.addObject("board", board);
//			modelAndView.setViewName("board/boardmodify");
//	
//			return modelAndView;		
//	
//		}
//		@RequestMapping(params = "action=modify")
//		public ModelAndView modify(HttpServletRequest request, HttpServletResponse response, Board board){
//	
//			boardService.modify(board);
//	
//	
//			board = boardService.view(board.getBoardNo());
//			ModelAndView modelAndView = new ModelAndView();
//			modelAndView.addObject("board", board);
//			modelAndView.setViewName("redirect:main.spring");
//			return modelAndView;
//	
//		}
//
//	@RequestMapping(params = "action=boarditemslist")
//	public ModelAndView BoardItemsList(HttpServletRequest request, HttpServletResponse response){
//
//		List<Boarditems> boarditemsList = boardItemsService.list2();
//
//		ModelAndView modelAndView = new ModelAndView();
//		
//		modelAndView.addObject("boardItems", boarditemsList);
//		
//		modelAndView.setViewName("bbs/freeBBS");
//		
//		return modelAndView;
//		//		String searchTitle= request.getParameter("searchCondition");
//		//		String searchCondition=request.getParameter("searchTitle");
//		//		
//		//		if(currentPage == null){
//		//			currentPage = 1;
//		//		}
//		//		
//		//		List<Boarditems> boardItemsList = boardItemsService.list(boardNo, searchCondition, searchTitle, currentPage);
//		//
//		//		
//		//		int totalCount=boardItemsService.countBoardItem(boardNo, searchCondition, searchTitle);
//		//		
//		//		Board board=boardService.view(boardNo);
//		//		
//		//		String paging= generatePaging(boardNo, currentPage, totalCount, searchCondition, searchTitle);
//		//		
//		//		ModelAndView modelAndView = new ModelAndView();
//		//		
//		//		modelAndView.addObject("boardItems", boardItemsList);
//		//		
//		//		modelAndView.addObject("board", board);
//		//		
//		//		modelAndView.addObject("paging", paging);
//		//		
//		//		modelAndView.setViewName("bbs/freeBBS");
//		//		return modelAndView;
//
//	}
//
//	@RequestMapping(params = "action=itemsView")
//	public ModelAndView boardItemsView(HttpServletRequest request, HttpServletResponse response,long boardItemsNo){
//
//		Boarditems boarditems=boardItemsService.count(boardItemsNo);
//
//		ModelAndView modelAndView = new ModelAndView();
//
//		modelAndView.addObject("boardItems", boarditems);
//
//		modelAndView.setViewName("bbs/boarditemsView");
//
//		return modelAndView;
//
//	}
//
//	@RequestMapping(params = "action=boardItemsImagelist")
//	public ModelAndView boardItemsImagelist(HttpServletRequest request, HttpServletResponse respons){
//
//		//List<Boarditems> boardItemsList = boardItemsService.list(0, null, null, 0);
//
//		ModelAndView modelAndView = new ModelAndView();
//
//		//modelAndView.addObject("boardItems", boardItemsList);
//
//		modelAndView.setViewName("bbs/albumBBS");
//
//		return modelAndView;
//
//	}
//
//	@RequestMapping(params = "action=boardItemsPremodify")
//	public ModelAndView boardItemsPremodify(long boardItemsNo){
//
//		Boarditems boarditems=boardItemsService.view(boardItemsNo);
//
//		ModelAndView modelAndView = new ModelAndView();
//
//		modelAndView.addObject("boardItems",boarditems);
//
//		modelAndView.setViewName("bbs/boarditemsModify");
//
//		return modelAndView;
//
//	}
//	@RequestMapping(params="action=boardItemsModify")
//	public ModelAndView boardItemsModify(Boarditems boarditems)
//	{
//		boarditems.setUpdateDate(new Timestamp(System.currentTimeMillis()));
//
//		if (boarditems.getBoarditemsRemoveYn()==null) {
//
//			boarditems.setBoarditemsNoticeYn("N");
//
//		} else {
//
//			boarditems.setBoarditemsNoticeYn("Y");
//
//		}
//
//		boolean result=boardItemsService.modify(boarditems);
//
//		ModelAndView modelAndView =new ModelAndView();
//
//		if (result) {
//
//			boarditems=boardItemsService.view(boarditems.getBoarditemsNo());
//
//			modelAndView.addObject("boarditems",boarditems);
//
//			modelAndView.setViewName("bbs/boarditemsModify");
//
//		} else {
//
//		}
//		return modelAndView;
//
//	}
//
//	@RequestMapping(params = "action=boardItemsPreAdd")
//	public ModelAndView boarditemsPreAdd(HttpServletRequest request, HttpServletResponse respons){
//
//		ModelAndView modelAndView = new ModelAndView();
//
//		modelAndView.setViewName("bbs/boarditems");
//
//		return modelAndView;
//
//	}
//
//
//	@RequestMapping(params = "action=boardItemsAdd")
//	public ModelAndView boarditemsAdd(HttpServletRequest request, HttpServletResponse respons,Boarditems boarditems){
//
//		Member member = (Member)request.getSession().getAttribute("login");
//
//		boarditems.setMemberId(member.getMemberId());
//
//		if(boarditems.getBoarditemsNoticeYn() == null){
//			boarditems.setBoarditemsNoticeYn("N");
//		}
//		else{
//			boarditems.setBoarditemsNoticeYn("Y");
//		}
//
//		boarditems.setBoarditemsViewCnt(0);
//		boarditems.setBoarditemsRemoveYn("N");
//		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
//
//
//		boarditems.setUpdateDate(timestamp);
//		boarditems.setInputDate(timestamp);
//
//		boolean result = boardItemsService.add(boarditems);
//
//		if(result){
//
//			int currentPage = 0;
//
//			String strCurrentPage = request.getParameter("currenPage");
//
//			if(strCurrentPage != null){
//				currentPage = Integer.parseInt(strCurrentPage);
//			}
//
//			return BoardItemsList(request, respons);
//		}
//		else{
//
//			ModelAndView modelAndView = new ModelAndView();
//			//modelAndView.addObject("message", "입력에 실패하였습니다.");
//			modelAndView.setViewName("bbs/freeBBS");
//
//			return modelAndView;
//		}
//		//		ModelAndView modelAndView = new ModelAndView();
//		//
//		//		modelAndView.setViewName("bbs/freeBBS");
//		//		
//		//		return modelAndView;
//
//	}
//